package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/userTest")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public List<User> getUsers() {
        // 模拟当前用户为 U1
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("U1", "password", "ROLE_USER"));

        // 模拟当前用户角色为 DATA_OPERATOR
        when(remoteRoleService.currentUserRoleType()).thenReturn(Collections.singleton("DATA_OPERATOR"));

        return userService.list();
    }

    @GetMapping("/users/page")
    public IPage<User> getUsersPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {

        // 模拟当前用户为 U4
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("U4", "password", "ROLE_USER"));

        // 模拟当前用户角色为 DATA_AUDITOR
        when(remoteRoleService.currentUserRoleType()).thenReturn(Collections.singleton("DATA_AUDITOR"));

        IPage<User> page = new Page<>(currentPage, pageSize);
        return userService.page(page);
    }
}
